namespace ConcurrentBoo

import System.Threading

def cmap(items, function as Function):
"""
Concurrent map.
""" 
	for computation in cmap_(items, function):
		yield computation.Value
		
def cmapU(items, function as Function):
"""
Unordered concurrent map. Mapped items are yielded as they become available.
"""
	for computation in cmapU_(items, function):
		yield computation.Value

def cmap_(items, function as Function):
	computations = [future({ function(item) }) for item in items]
	for computation as Computation in computations:
		yield computation
		
def cmapU_(items, function as Function):
	channel = ComputationChannel()
	
	total = 0
	for item in items:
		channel.Send({ function(item) })
		++total
	
	while total > 0:
		--total
		yield channel.Receive()
		
